package com.zahb.qadx.util

import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners
import com.zahb.qadx.R

/**
 * Created by Zdw on 2021/06/30 17:44
 */

const val baseUrl = "http://qadx.oss-cn-hangzhou.aliyuncs.com"
const val defaultPlaceholder = R.mipmap.placeholder_figure
const val defaultError = R.mipmap.placeholder_figure

fun transformPath(path: String?): String {
    return path.run {
        when {
            path.isNullOrBlank() -> ""//path为null或""或只有空格
            path.startsWith("http") -> this as String//不需要补全链接
            path.startsWith("/") -> "$baseUrl$this"//需要补全链接
            else -> "$baseUrl/$this"//需要补全链接
        }
    }
}

//加载图片（没有圆角）
@JvmOverloads
fun ImageView.loadImage(path: String?, placeholder: Int = defaultPlaceholder, error: Int = defaultError) {
    loadImageSameCorners(path, 0f, placeholder, error)
}

//加载图片（相同圆角）
@JvmOverloads
fun ImageView.loadImageSameCorners(path: String?, roundingRadius: Float, placeholder: Int = defaultPlaceholder, error: Int = defaultError) {
    loadImageCorners(path, roundingRadius, roundingRadius, roundingRadius, roundingRadius, placeholder, error)
}

//加载图片（不同圆角）
@JvmOverloads
fun ImageView.loadImageCorners(path: String?, topLeft: Float = 0f, topRight: Float = 0f, bottomRight: Float = 0f, bottomLeft: Float = 0f,
                               placeholder: Int = defaultPlaceholder, error: Int = defaultError) {
    GlideApp.with(this).asBitmap().load(transformPath(path)).apply {
        if (topLeft > 0 || topRight > 0 || bottomRight > 0 || bottomLeft > 0)
            transform(GranularRoundedCorners(
                    topLeft.dp2px().toFloat(),
                    topRight.dp2px().toFloat(),
                    bottomRight.dp2px().toFloat(),
                    bottomLeft.dp2px().toFloat()))
        if (placeholder > 0) placeholder(placeholder)
        if (error > 0) error(error)
    }.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(this)
}

//加载图片（裁剪圆形）
@JvmOverloads
fun ImageView.loadImageCircle(path: String?, placeholder: Int = defaultPlaceholder, error: Int = defaultError) {
    GlideApp.with(this).asBitmap().load(transformPath(path)).circleCrop().apply {
        if (placeholder > 0) thumbnail(GlideApp.with(this@loadImageCircle).asBitmap().load(placeholder).transform(CircleCrop()))
        if (error > 0) error(GlideApp.with(this@loadImageCircle).asBitmap().load(error).transform(CircleCrop()))
    }.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(this)
}

//加载图片
@JvmOverloads
fun ImageView.loadImageFit(path: String?, placeholder: Int = 0, error: Int = 0) {
    GlideApp.with(this).asBitmap().load(transformPath(path)).fitCenter().apply {
        if (placeholder > 0) placeholder(placeholder)
        if (error > 0) error(error)
    }.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(this)
}

//加载图片
@JvmOverloads
fun ImageView.loadImageFit(@DrawableRes sourceId: Int, placeholder: Int = 0, error: Int = 0) {
    GlideApp.with(this).asBitmap().load(sourceId).fitCenter().apply {
        if (placeholder > 0) placeholder(placeholder)
        if (error > 0) error(error)
    }.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(this)
}

//加载图片
@JvmOverloads
fun ImageView.loadImageGifFit(@DrawableRes sourceId: Int, placeholder: Int = 0, error: Int = 0) {
    GlideApp.with(this).asGif().load(sourceId).fitCenter().apply {
        if (placeholder > 0) placeholder(placeholder)
        if (error > 0) error(error)
    }.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(this)
}

fun Float.dp2px(): Int {
    return SizeUtils.dp2px(this)
}
